496B - Secret Combination - CodeForces Solution


brute force constructive algorithms implementation *1500

Please click on ads to support us..

Python Code:

n=int(input())
num=input()

def right_shift(nb):
    nb=nb[n-1]+nb[:n-1]
    return nb

def add(s1,a):
    nb=''
    for i in range(n):
        nb+=str((int(s1[i])+a)%10)
    return nb

minS=num
for i in range(n):
    diff=10-int(num[0])
    num=add(num,diff)
    if(minS>num):
            minS=num
    num=add(num,10-diff)
    num=right_shift(num)
    if(minS>num):
        minS=num
print(minS)

C++ Code:

#include <iostream>

using namespace std;
  
int main(){
    int n;
	string s;
	cin >> n >> s;
    string b = s;

    for(int i = 0; i < 10; i++){
		for(int j = 0; j < n; j++)
            s[j] = (s[j] - '0' + 1) % 10 + '0';
        for(int j = 0;j < n; j++)	
			b = min(b , (s+s).substr(j , n));
	}
    cout << b;
    
    return 0;
}/*1698360782.0175083*/


Comments

Submit
0 Comments
More Questions

664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline